package com.revzone.demo001.PermissionModule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.revzone.demo001.LogModule.aspect.OperationLog;
import com.revzone.demo001.PermissionModule.mapper.MenuMapper;
import com.revzone.demo001.PermissionModule.service.MenuService;
import com.revzone.demo001.entity.OtherEntity.R;
import com.revzone.demo001.entity.PermissionEntity.Menu;
import com.revzone.demo001.enums.PermissionEnums.EnumOperationLog;
import com.revzone.demo001.enums.PermissionEnums.EnumTypeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lsa
 * @since 2023-03-27
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    @OperationLog(value = EnumTypeValue.USER_PERMISSIONS, description = EnumOperationLog.MENU_QUERY)
    public List<Menu> getMenuList(Integer parentId) {
        List<Menu> menus = new ArrayList<>();
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        List<Menu> children ;
        if(parentId == 0){
            children  = menuMapper.selectList(null);
        }else {
            menuQueryWrapper.eq("parent_id",parentId);
//            menuQueryWrapper.orderByAsc("sort");
            children  = menuMapper.selectList(menuQueryWrapper);
            if (children  == null || children .isEmpty()) { // 判断子菜单是否为空
                return menus ;
            }
            for (Menu child : children ) {
                Menu parent = this.getById(child.getParentId()); // 获取当前子级菜单的父级菜单
                child.setParent(parent); // 设置子级菜单的父级菜单属性
                while (parent != null) {  // 如果有父级菜单，则设置父级菜单的parent属性
                    // 获取当前父级菜单的父级菜单
                    Menu grandParent = this.getById(parent.getParentId());
                    // 设置当前父级菜单的parent属性
                    if (grandParent != null) {
                        parent.setParent(grandParent);
                    }
                    // 继续向上查找
                    parent = grandParent;
                }

                List<Menu> subMenus = this.getMenuList(child.getId().intValue()); // 递归查询子级菜单的子级菜单
                child.setChildren(subMenus); // 设置子级菜单的子级菜单列表属性
                menus.add(child); // 添加子级菜单到结果集合
            }
        }
        return menus ;
    }

    @Override
    public List<Menu> getMenuListById(Integer id) {
        List<Menu> menus = new ArrayList<>();
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        List<Menu> children ;
        if(id == 0){
            children  = menuMapper.selectList(null);
        }else {
            menuQueryWrapper.eq("id",id);
            menuQueryWrapper.orderByAsc("sort");
            children  = menuMapper.selectList(menuQueryWrapper);
            if (children  == null || children .isEmpty()) { // 判断子菜单是否为空
                return menus ;
            }
            for (Menu child : children ) {
                Menu parent = this.getById(child.getParentId()); // 获取当前子级菜单的父级菜单
                child.setParent(parent); // 设置子级菜单的父级菜单属性
                while (parent != null) {  // 如果有父级菜单，则设置父级菜单的parent属性
                    // 获取当前父级菜单的父级菜单
                    Menu grandParent = this.getById(parent.getParentId());
                    // 设置当前父级菜单的parent属性
                    if (grandParent != null) {
                        parent.setParent(grandParent);
                    }
                    // 继续向上查找
                    parent = grandParent;
                }

                List<Menu> subMenus = this.getMenuList(child.getId().intValue()); // 递归查询子级菜单的子级菜单
                child.setChildren(subMenus); // 设置子级菜单的子级菜单列表属性
                menus.add(child); // 添加子级菜单到结果集合
            }
        }
        return menus ;
    }

    @Override
    @OperationLog(value = EnumTypeValue.USER_PERMISSIONS, description = EnumOperationLog.MENU_ADDITION)
    public R setMenuList(Menu menu) {
        menu.setCreateDate(new Date());
        menu.setUpdateDate(new Date());
        int insert = menuMapper.insert(menu);
        if (insert == 0){
            return R.error("Failure");
        }else {
            return R.ok("success");
        }
    }

    @Override
    @OperationLog(value = EnumTypeValue.USER_PERMISSIONS, description = EnumOperationLog.MENU_MODIFICATION)
    public R updateMenuList(Menu menu) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",menu.getId());
        menu.setUpdateDate(new Date());
        int update = menuMapper.update(menu, queryWrapper);
        if (update == 0){
            return R.error("Failure");
        }else {
            return R.ok("success");
        }
    }

    @Override
    @OperationLog(value = EnumTypeValue.USER_PERMISSIONS, description = EnumOperationLog.MENU_DELETION)
    public R delMenuList(Integer id) {
        int delete = menuMapper.deleteById(id);
        if (delete == 0){
            return R.error("Failure");
        }else {
            return R.ok("success");
        }
    }

}
